package cn.edu.scau.cmi.liyunlin.hibernate.contorller;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.liyunlin.hibernate.dao.StudentDao;
import cn.edu.scau.cmi.liyunlin.hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.liyunlin.hibernate.domain.Student;
import cn.edu.scau.cmi.liyunlin.hibernate.domain.Teacher;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.TextArea;

public class HibernateViewController implements Initializable {
	@FXML
	private TextField stuAddId;
	@FXML
	private TextField stuAddName;
	@FXML
	private Button stuAddBtn;
	@FXML
	private TextField stuDelId;
	@FXML
	private Button stuDelBtn;
	@FXML
	private Button stuUpdateBtn;
	@FXML
	private TextField stuUpdName;
	@FXML
	private TextField stuUpdId;
	@FXML
	private TextField teaUpdId;
	@FXML
	private TextField teaUpdName;
	@FXML
	private Button teaUpdBtn;
	@FXML
	private Button teaDelBtn;
	@FXML
	private TextField teaDelId;
	@FXML
	private Button teaAddBtn;
	@FXML
	private TextField teaAddName;
	@FXML
	private TextField teaAddId;
	@FXML
	private Button stuChooseBtn;
	@FXML
	private TextField stuChooseTid;
	@FXML
	private TextField stuChooseSid;
	@FXML
	private TextField stuUpdSid;
	@FXML
	private TextField stuUpdTid;
	@FXML
	private Button stuUpdBtn;
	@FXML
	private Button TeaUpdBtn;
	@FXML
	private TextField TeaUpdSid;
	@FXML
	private TextField TeaUpdTid;
	@FXML
	private TextField TeaChooseTid;
	@FXML
	private TextField TeaChooseSid;
	@FXML
	private Button TeaChooseBtn;
	@FXML
	private TextArea msgBox;
	
	StudentDao studentDao;
	TeacherDao teacherDao;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		studentDao = new StudentDao();
		teacherDao = new TeacherDao();
	}
	
	/**
	 * 添加学生
	 */
	public void addStudent(ActionEvent event) {
		Long id = Long.parseLong(stuAddId.getText());
		String stuName =  stuAddName.getText();
		Student student = new Student();
		student.setName(stuName);
		student.setId(id);
		studentDao.save(student);
		
		msgBox.setText(stuName + "学生添加成功:" + student.toString() + "\n");
	}
	
	/**
	 * 删除学生
	 */
	public void deleteStudent(ActionEvent event) {
        Long id = Long.parseLong(stuDelId.getText());
		
		Student stu = studentDao.findById(id);
		if(stu != null) {
			msgBox.setText(stu.getName() + stu.getId());
			studentDao.delete(stu);
			msgBox.setText(id + "学生删除成功：" + stu.toString()+" \n");
		}else {
			msgBox.setText("学生不存在\n");
		}
	}
	
	/**
	 * 更新学生信息
	 */
	public void updateStudent(ActionEvent event) {
		Long id = Long.parseLong(stuUpdId.getText());
		String newName = stuUpdName.getText();
		Student stu = studentDao.findById(id);
		if(stu != null) {
			stu.setName(newName);
			stu = studentDao.merge(stu);
			msgBox.setText("学生更新成功：" + stu.toString()+"\n");
		}else {
			msgBox.setText("学生不存在\n");
		}
	}
	
	/**
	 * 添加教师
	 */
	public void addTeacher(ActionEvent event) {
		Long id = Long.parseLong(teaAddId.getText());
		String name = teaAddName.getText();
		Teacher teacher = new Teacher();
		teacher.setName(name);
		teacher.setId(id);
		teacherDao.save(teacher);;
		
		msgBox.setText("教师添加成功：" + teacher.toString()+"\n");
	}
	
	/**
	 * 删除教师
	 */
	public void deleteTeacher(ActionEvent event) {
        Long id = Long.parseLong(teaDelId.getText());
		
		Teacher teacher = teacherDao.findById(id);
		if(teacher != null) {
			msgBox.setText(teacher.getName() + teacher.getId());
			teacherDao.delete(teacher);
			msgBox.setText(id + "教师删除成功：" + teacher.toString()+" \n");
		}else {
			msgBox.setText("教师不存在\n");
		}
	}
	
	/**
	 * 更新教师信息
	 */
	public void updateTeacher(ActionEvent event) {
		Long id = Long.parseLong(teaUpdId.getText());
		String newName = teaUpdName.getText();
		Teacher teacher = teacherDao.findById(id);
		if(teacher != null) {
			teacher.setName(newName);
			teacher = teacherDao.merge(teacher);
			msgBox.setText("教师更新成功：" + teacher.toString()+"\n");
		}else {
			msgBox.setText("教师不存在\n");
		}
	}
	
	/**
	 * 学生选择导师
	 */
	public void studentChooseTeacher(ActionEvent event) {
		Long studentId = Long.parseLong(stuChooseSid.getText());
		Long teacherId = Long.parseLong(stuChooseTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}
		Teacher teacher = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}
		stu.setTeacher(teacher);
		studentDao.attachDirty(stu);
		msgBox.setText("学生 " + stu.toString()+" 绑定教师 "+teacher+" 成功\n");
	}
	
	/**
	 * 学生更换绑定导师
	 */
	public void studentUpdateTeacher(ActionEvent event) {
		Long studentId = Long.parseLong(stuUpdSid.getText());
		Long teacherId = Long.parseLong(stuUpdTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}
		Teacher teacher = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}
		stu.setTeacher(teacher);
		studentDao.attachDirty(stu);
		msgBox.setText("学生 " + stu.toString()+" 绑定教师 "+teacher+" 成功\n");
	}
	
	/**
	 * 教师选择指导学生
	 */
	public void teacherChooseStudent(ActionEvent event) {
		Long studentId = Long.parseLong(TeaChooseSid.getText());
		Long teacherId = Long.parseLong(TeaChooseTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}else if(stu.getTeacher() != null) {
			msgBox.setText("学生已有指导教师\n");
			return;
		}
		Teacher teacher = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}
		stu.setTeacher(teacher);
		studentDao.attachDirty(stu);
		msgBox.setText("教师 " + teacher+" 绑定学生 "+stu.toString()+" 成功\n");
	}
	
	/**
	 * 教师取消绑定学生
	 */
	public void teacherCancelStudent(ActionEvent event) {
		Long studentId = Long.parseLong(TeaUpdSid.getText());
		Long teacherId = Long.parseLong(TeaUpdTid.getText());
		
		Student stu = studentDao.findById(studentId);
		if(stu == null) {
			msgBox.setText("学生不存在\n");
			return;
		}
		Teacher teacher = stu.getTeacher();
		Teacher teacher1 = teacherDao.findById(teacherId);
		if(teacher == null) {
			msgBox.setText("教师不存在\n");
			return;
		}else if(teacher.getId().longValue() != teacher1.getId().longValue()) {
			msgBox.setText("您不是 " + teacher.getId() +" 无法对学生 "+ stu.getId() +"操作\n");
			return;
		}
		stu.setTeacher(null);
		studentDao.attachDirty(stu);
		msgBox.setText("教师 " + teacher.getId() +" 取消绑定学生 "+stu.toString()+" 成功\n");
	}

}
